package zhoug.chart.markers;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import zhoug.chart.Chart2Sdk;
import zhoug.chart.R;
import zhoug.chart.core.MPConstant;
import zhoug.chart.utils.MPChartUtils;
import zhoug.chart.views.MPIconView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 柱状图曲线图混合图表marker:支持单个,堆叠,并排
 *
 * @Author 35574
 * @Date 2022/1/5
 * @Description
 */
public class MPCombinedMarker extends MPBaseMarker {
    private static final String TAG = ">>>BarMarker";
    protected static final boolean DEBUG = Chart2Sdk.isDebug();
    public static final int SHOW_TYPE_HIGHLIGHT = 1;//只显示点击的高亮的数据
    public static final int SHOW_TYPE_HIGHLIGHT_DATA_SET = 2;//只显示点击的高亮的DataSet
    public static final int SHOW_TYPE_GROUP = 3;// 点击的那个组

    public static final int PERCENT_TYPE_BAR_SELF = 1;//
    public static final int PERCENT_TYPE_BAR_GROUP = 2;//
    public static final int PERCENT_TYPE_LINE_GROUP = 3;//

    public static final int SUM_BAR_DATA_SET = 0;//显示bar的y和
    public static final int SUM_BAR_GROUP = 1;//显示bar的y和
    public static final int SUM_LINE = 3;//显示line的y和
    public static final int SUM_GROUP = 4;//显示全部y的和
    /**
     * {@link #SHOW_TYPE_HIGHLIGHT,#SHOW_TYPE_HIGHLIGHT_DATA_SET,#SHOW_TYPE_GROUP,}
     */
    protected int mShowType = SHOW_TYPE_GROUP;//
    /**
     * {@link #PERCENT_TYPE_BAR_SELF,#PERCENT_TYPE_BAR_GROUP,#PERCENT_TYPE_LINE_GROUP,}
     */
    protected int mPercentType = PERCENT_TYPE_BAR_GROUP;
    /**
     * {@link #SUM_BAR_DATA_SET,#SUM_BAR_GROUP,#SUM_LINE,#SUM_GROUP}
     */
    protected int mSumType = SUM_BAR_GROUP;
    protected boolean mShowSum = false;//是否在label后面显示总数
    protected OnBindDataListener mOnBindDataListener;

    protected TextView mTvXValue;
    protected TextView mTvSumValue;
    protected MPIconView mIvXValueIcon;
    protected RecyclerView mRecyclerView;
    protected Adapter mAdapter;


    public MPCombinedMarker(Context context) {
        this(context, R.layout.mpchart_list_marker);
        mIvXValueIcon = findViewById(R.id.iv_xValueIcon);
        mTvXValue = findViewById(R.id.tv_xValue);
        mTvSumValue = findViewById(R.id.tv_sumValue);
        mRecyclerView = findViewById(R.id.recyclerView);
        mAdapter = new Adapter();
        mAdapter.setData(new ArrayList<>());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        mRecyclerView.setAdapter(mAdapter);
    }

    public MPCombinedMarker(Context context, int layoutResource) {
        super(context, layoutResource);
    }

    @Override
    public void onBindView(Entry entryForHighlight, Highlight highlight, @NonNull MPMarkerData markerData) {
        List<MPMarkerData.YData> list;
        String xLabel = markerData.getXLabel();
        List<MPMarkerData.YData> yData = markerData.getData();
        if (mShowType == SHOW_TYPE_GROUP) {
            list = yData;
        } else {
            list = new ArrayList<>();
            for (MPMarkerData.YData item : yData) {
                if (mShowType == SHOW_TYPE_HIGHLIGHT) {
                    if (item.getDataSetIndex() == markerData.getDataSetIndex() && item.getStackIndex() == markerData.getStackIndex()) {
                        list.add(item);
                        break;
                    }
                } else if (mShowType == SHOW_TYPE_HIGHLIGHT_DATA_SET) {
                    if (item.getDataSetIndex() == markerData.getDataSetIndex()) {
                        list.add(item);
                    }
                }
            }
        }
        List<MPMarkerData.YData> data = mAdapter.getData();
        data.clear();
        float sum = 0;
        if (list.size() > 0) {
            if (mShowSum) {
                boolean addBar = false;
                boolean addLine = false;
                for (MPMarkerData.YData item : list) {
                    int chartType = item.getChartType();
                    int dataSetIndex = item.getDataSetIndex();
                    if (chartType == MPConstant.BAR_CHART) {
                        if (mSumType == SUM_BAR_DATA_SET) {
                            if (dataSetIndex == markerData.getDataSetIndex()) {
                                sum = item.getSelfSum();
                                break;
                            }
                        } else if (mSumType == SUM_BAR_GROUP) {
                            sum = item.getGroupSum();
                            break;
                        } else if (mSumType == SUM_GROUP) {
                            if (!addBar) {
                                addBar = true;
                                sum += item.getGroupSum();
                            }
                        }
                    } else if (chartType == MPConstant.LINE_CHART) {
                        if (mSumType == SUM_LINE) {
                            sum = item.getGroupSum();
                            break;
                        } else if (mSumType == SUM_GROUP) {
                            if (!addLine) {
                                addLine = true;
                                sum += item.getGroupSum();
                            }
                        }
                    }
                }
            }
            data.addAll(list);
        }
        calculateMaxLabelLength(data,xLabel);
        if(mOnBindDataListener==null || !mOnBindDataListener.onBindXLabel(mTvXValue,xLabel,mShowSum,sum)){
            if (mShowSum) {
                mTvXValue.setText(getLabelFormat(xLabel));
                mTvSumValue.setText(getFormattedValue(sum));
            }else{
                mTvXValue.setText(xLabel);
            }
        }
        if (mShowSum) {
            mTvSumValue.setVisibility(VISIBLE);
        } else{
            mTvSumValue.setVisibility(GONE);
        }
        mAdapter.notifyDataSetChanged();
    }


    public class Adapter extends MPBaseRecyclerViewAdapter<MPMarkerData.YData> {
        public Adapter() {
        }

        @Override
        protected int getLayoutId(int viewType) {
            if (mOnBindDataListener != null && mOnBindDataListener.getListItemLayoutId() != 0) {
                return mOnBindDataListener.getListItemLayoutId();
            }
            return R.layout.mpchart_marker_item;
        }

        @Override
        protected void onBindData(MPBaseViewHolder holder, MPMarkerData.YData data, int position, int viewType) {
            if (data != null) {
                MPIconView mpIcon = holder.getView(R.id.iv_icon);
                TextView tvLabel = holder.getView(R.id.tv_label);
                TextView tvValue = holder.getView(R.id.tv_value);
                boolean bind= mOnBindDataListener == null || !mOnBindDataListener.onBindItemData(holder, data, position, viewType);
                if (bind) {
                    onBindItemData(mpIcon,tvLabel,tvValue,data);
                }
            }
        }

    }

    protected void onBindItemData(MPIconView mpIcon,TextView tvLabel,TextView tvValue,MPMarkerData.YData data) {
        mpIcon.setColor(data.getColor());
        int chartType = data.getChartType();
        if (chartType == MPConstant.LINE_CHART) {
            mpIcon.setShape(MPIconView.LINE);
        } else if (chartType == MPConstant.BAR_CHART) {
            mpIcon.setShape(MPIconView.RECTANGLE);
        } else if (chartType == MPConstant.PIE_CHART) {
            mpIcon.setShape(MPIconView.CIRCLE);
        }
        tvLabel.setText(getLabelFormat(data.getLabel()));
        boolean isSetValue = false;
        if (mShowPercent) {
            if (chartType == MPConstant.LINE_CHART && mPercentType == PERCENT_TYPE_LINE_GROUP) {
                tvValue.setText(MPChartUtils.formatString("%s  (%s%%)", data.getFormatValue(), getPercentValue(data.getGroupRatio())));
                isSetValue = true;
            } else if (chartType == MPConstant.BAR_CHART) {
                if (mPercentType == PERCENT_TYPE_BAR_SELF) {
                    tvValue.setText(MPChartUtils.formatString("%s  (%s%%)", data.getFormatValue(), getPercentValue(data.getSelfRatio())));
                    isSetValue = true;
                } else if (mPercentType == PERCENT_TYPE_BAR_GROUP) {
                    tvValue.setText(MPChartUtils.formatString("%s  (%s%%)", data.getFormatValue(), getPercentValue(data.getGroupRatio())));
                    isSetValue = true;
                }
            }
        }
        if (!isSetValue) {
            tvValue.setText(data.getFormatValue());
        }
        mpIcon.postInvalidate();
    }

    /**
     * {@link #SHOW_TYPE_HIGHLIGHT,#SHOW_TYPE_HIGHLIGHT_DATA_SET,#SHOW_TYPE_GROUP,}
     */
    public void setShowType(int showType) {
        this.mShowType = showType;
    }

    public void showSum(boolean showSum) {
        this.mShowSum = showSum;
    }

    /**
     * {@link #PERCENT_TYPE_BAR_SELF,#PERCENT_TYPE_BAR_GROUP,#PERCENT_TYPE_LINE_GROUP,}
     */
    public void setPercentType(int percentType) {
        this.mPercentType = percentType;
    }

    /**
     * {@link #SUM_BAR_DATA_SET,#SUM_BAR_GROUP,#SUM_LINE,#SUM_GROUP}
     */
    public void setSumType(int sumType) {
        this.mSumType = sumType;
    }

    public void setOnBindDataListener(OnBindDataListener onBindDataListener) {
        mOnBindDataListener = onBindDataListener;
    }




    public interface OnBindDataListener {
        /**
         * x轴label数据绑定
         * @param tvXLabel
         * @param xLabel
         * @param showSum
         * @param sum
         * @return false 使用默认的
         */
        boolean onBindXLabel(TextView tvXLabel,String xLabel, boolean showSum, float sum);

        /**
         * listItem布局文件
         * @return
         */
        @LayoutRes
        int getListItemLayoutId();

        /**
         * listItem布局文件数据绑定
         * @param holder
         * @param data
         * @param position
         * @param viewType
         */
        boolean onBindItemData(MPBaseViewHolder holder, MPMarkerData.YData data, int position, int viewType);
    }


}
